package registnet.base.source;

import java.util.List;
import java.util.ListIterator;

import registnet.base.CourseTaken;
import registnet.base.Form5;
import registnet.dao.Grade;
import registnet.dao.GradePeer;
import registnet.dao.Student;
import registnet.dao.StudentPeer;

public class Form5Source implements IForm5Source {

	private Form5 form5;
	private String studentNumber;
	private int year;
	private int semester;
	
	
	public Form5Source(String studentNumber, int semester, int year){
		this.studentNumber = studentNumber;
		this.semester = semester;
		this.year = year;
		this.load();
	}
	
	public void load() {
		Student student = new StudentPeer().getStudent(this.studentNumber);
		this.form5 = new Form5(this.studentNumber, this.semester, this.year);
		List<Grade> grades = new GradePeer().getGradeByStudent(student.getStudentId(), this.semester, this.year);
		ListIterator<Grade> iter = grades.listIterator();
		while(iter.hasNext()){
			try{
				Grade grade = iter.next();
				CourseTaken course = new CourseTaken(grade.getCourse().getCourseCode(), this.semester, this.year);
				
				course.setGrade(grade.getGrade());
				course.setCourseName(grade.getCourse().getCourseName());
				course.setSectionName(grade.getSectionName());				
				course.setUnit(grade.getCourse().getUnits());
				course.setCredit(grade.getCredit());
				course.setRemarks(grade.getRemarks());
				
				String courseCode = grade.getCourse().getCourseCode();
				
				
				if (!grade.getSectionName().equals("REMOVAL") && !grade.getSectionName().equals("COMPLETION")){
					this.form5.addCourse(course);
					
					if (grade.getGrade() == 4){
						this.form5.getAllCourses().get(courseCode).setReExam(new GradePeer().getRemovalGrade(grade));
					}
					
					else if (grade.getGrade() == 7){
						this.form5.getAllCourses().get(courseCode).setReExam(new GradePeer().getCompletionGrade(grade));
					}	
					
				}
				
			} catch(Exception e) { e.printStackTrace(); }
		}
	}
	
	public Form5 getForm5(){
		return this.form5;
	}
	


}
